<template>
    <div class="login">
        <div class="imgBox">
            <van-icon name="arrow-left" size="20px" @click="goback()" />
        </div>
        <div class="title">
            设置密码
        </div>
        <van-cell-group>
            <van-field v-model="password1" required label="设置密码" placeholder="密码长度为6-12位数字" type="password" />

            <van-field v-model="password2" type="password" label="确认密码" placeholder="密码长度为6-12位数字" required />
        </van-cell-group>

        <div class="denglu" @click="setpassword()">
            确定
        </div>
    </div>
</template>

<script>
    import { mapMutations } from 'vuex'
    import { Toast } from "vant"
    import { Field } from 'vant';
    import { axios } from "axios"
    export default {
        data() {
            return {
                username: "",
                password1: "",
                password2: "",
                type: ""
            }
        },
        methods: {
            // 如果methods里面还有其他函数，就要在辅助函数名称的前面加上扩展运算符
            ...mapMutations(['changeToken']),

            setpassword() {

                var reg = /^\d{6,12}$/;
                console.log("yonghum", this.username, "mima", this.password1)
                let user = this.username;
                let psd = this.password1;
                let type = this.type

                //var that=this;


                if (!reg.test(this.password1)) {
                    Toast({
                        message: '请输入长度6-12位数字密码', // 显示内容
                        duration: 1000 // 显示时间
                    })
                    return false;
                } else {
                    if (this.password1 == this.password2) {
                        // 验证通过
                        // alert("验证通过")
                        localStorage.getItem("type",type)
                        console.log(type)
                        if (type=="忘记密码") {
                            console.log("调取重新重置密码请求")
                            this.$axios.post("/forget/update/password", {
                                phone: user,
                                password: psd
                            }).then((res) => {
                                console.log(res);
                                const { status, message, data } = res

                                if (status == 200) {
                                    Toast("密码设置成功")
                                    console.log(777,data)

                                    this.$router.push(
                                        {
                                            name: 'start',
                                        }
                                    )
                                } else {
                                    Toast("cuowu")
                                }
                            })

                        } else {
                            console.log(type)
                            console.log("调取设置密码接口")
                            this.$axios.get("/user/register", {
                                params:{
                                    login_name: user,
                                password: psd
                                }
                            }).then((res) => {
                                console.log(res);
                                const { status, message, data } = res

                                if (status == 200) {
                                    Toast(message)
                                    console.log(data)

                                    this.$router.push(
                                        {
                                            name: 'start',
                                        }
                                    )
                                } else {
                                    Toast("cuowu")
                                }
                            })
                        }



                    } else {
                        Toast({
                            message: '两次密码不一致', // 显示内容
                            duration: 1000 // 显示时间
                        })
                        return false;
                    }
                }
            },
            goback() {
                this.$router.back()
            }
        },
        mounted() {
            // alert(666)
            // this.time()
            this.username = this.$route.params.username;
            this.type = this.$route.params.type;
            console.log(222, this.username, this.type)
        }
    }
</script>

<style lang="scss" scoped>
    .login {
        padding-top: 30px;
    }

    .imgBox {
        position: fixed;
        left: 4px;
        top: 10px;
        overflow: hidden;
        width: 24px;
        height: 20px;
    }

    .goback {
        width: 20px;
        height: 24px;
    }

    .title {

        margin: 20px 0;
        margin-left: 12px;
        width: 115px;
        height: 30px;
        color: rgba(16, 16, 16, 1);
        font-size: 28px;
        text-align: left;
        font-family: SourceHanSansSC-bold;
        /* font-weight:900; */
    }

    #username,
    #password {
        width: 99%;
        height: 36px;
        margin: 0 auto;
    }

    .cell {
        text-align: center;
        position: relative;


    }

    .cell img {
        width: 20px;
        height: 20px;
        position: absolute;
        right: 4px;
        top: 8px;
    }

    .denglu {
        margin: 0 auto;
        margin-top: 30px;
        width: 99%;
        height: .96rem;
        border-radius: 4px;
        background-color: #07c160;
        color: rgba(255, 255, 255, 1);
        font-size: 20px;
        text-align: center;
        font-family: Microsoft Yahei;
        border: 1px solid rgba(187, 187, 187, 1);
        line-height: .96rem;
        max-height: 48px;
        display: flex;
        text-align: center;
        align-items: center;
        justify-content: center;
    }

    .forget {
        margin-left: 6px;
        margin-top: 8px;
        width: 70px;
        height: 20px;
        color: #999;
        font-size: 14px;
        text-align: left;
        font-family: SourceHanSansSC-regular;
    }
</style>